package com.xindacloud.sms.adapter.service.business.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xindacloud.sms.adapter.dao.SmsAgentGroupMapper;
import com.xindacloud.sms.adapter.entity.SmsAgentGroup;
import com.xindacloud.sms.adapter.exception.ServiceException;
import com.xindacloud.sms.adapter.service.business.ISmsAgentGroupService;
import com.xindacloud.sms.adapter.utils.UnionResultHelp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Optional;


@Slf4j
@Service
public class SmsAgentGroupServiceImpl extends ServiceImpl<SmsAgentGroupMapper, SmsAgentGroup> implements ISmsAgentGroupService {

    @Override
    public SmsAgentGroup queryOneAgentInfoByName(String groupName) {
        final SmsAgentGroup agentGroup = lambdaQuery()
                .eq(SmsAgentGroup::getStatus, true)
                .eq(SmsAgentGroup::getGroupName, groupName)
                .one();
        return checkResult(agentGroup);
    }

    @Override
    public SmsAgentGroup queryOneAgentInfoById(String groupId) {
        final SmsAgentGroup agentGroup = lambdaQuery()
                .eq(SmsAgentGroup::getStatus, true)
                .eq(SmsAgentGroup::getGroupId, groupId)
                .one();
        return checkResult(agentGroup);
    }

    private SmsAgentGroup checkResult(SmsAgentGroup agentGroup) {
        return Optional.ofNullable(agentGroup)
                .orElseThrow(() -> new ServiceException("通道组被禁用或不存在.", UnionResultHelp.StatusEnum.NotFound.code()));
    }
}
